package org.drawlineapi.tools {
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	
	public class AbsLine extends Sprite {
				
		private var _from:Point;
		private var _to:Point;
		
		private var _format:LineFormat = new LineFormat ();
				
		public function AbsLine(from:Point, to:Point) {
			
			if (getQualifiedClassName (this) == "org.drawlineapi.tools::AbsLine") {
				throw new Error ("A classe abstrata org.drawlineapi.tools.AbsLine não pode ser instanciada.");
				return;
			}
			
			this.from = from;
			this.to = to;
			
			this.update ();
			
			this.showShadow = true;
			
			this.addEventListener (MouseEvent.MOUSE_DOWN, onDown);
		}
		
		private function onDown(e:MouseEvent):void 
		{
			this.addEventListener (MouseEvent.MOUSE_MOVE, onMove);
			this.addEventListener (MouseEvent.MOUSE_UP, onUp);
			this.stage.addEventListener (MouseEvent.MOUSE_UP, onUp);
			this.stage.addEventListener (MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function onUp(e:MouseEvent):void 
		{			
			this.removeEventListener (MouseEvent.MOUSE_MOVE, onMove);
			this.removeEventListener (MouseEvent.MOUSE_UP, onUp);
			this.stage.removeEventListener (MouseEvent.MOUSE_UP, onUp);
			this.stage.removeEventListener (MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function onMove(e:MouseEvent):void {
			
		}
		
		public function set showShadow (value:Boolean):void {
			var shadow:DropShadowFilter = new DropShadowFilter (4, 45, 0, 0.5);
			if (value) {
				this.filters = [shadow];
			} else {
				this.filters = [];
			}
			
		}
		
		protected function lineFormat (f:LineFormat):void {
			this.graphics.lineStyle (f.thickness, f.color, f.alpha, true);
		}
		
		public function draw (from:Point, to:Point):void {
			this.graphics.moveTo (from.x, from.y);
			this.graphics.lineTo (to.x, to.y);			
		}
		
		public function remove ():void {
			this.parent.removeChild (this);
		}
		
		protected function update():void {
			this.graphics.clear ();
			this.lineFormat (this.format);
			if (this.from != null && this.to != null) {
				this.draw (this.from, this.to);
			}
		}
		
		public function get from():Point {
			return _from;
		}
		
		public function set from(value:Point):void {
			_from = value;
			this.update ();
		}
		
		public function get to():Point {
			return _to;
		}
		
		public function set to(value:Point):void {
			_to = value;
			this.update ();
		}
		
		public function get format():LineFormat {
			return _format;
		}
		
		public function set format(value:LineFormat):void {
			_format = value;
			this.lineFormat (value);
		}
		
		
		
	}

}